package com.happyfamily.springboot.crowdsourcingplatform.service.sun.impl;

import com.happyfamily.springboot.crowdsourcingplatform.dao.sun.AllianceDao;
import com.happyfamily.springboot.crowdsourcingplatform.dao.sun.OrderDao;
import com.happyfamily.springboot.crowdsourcingplatform.dao.sun.TenderDao;
import com.happyfamily.springboot.crowdsourcingplatform.model.Alliance;
import com.happyfamily.springboot.crowdsourcingplatform.model.User_alliance;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.AllianceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class AllianceServiceImpl implements AllianceService
{
	@Autowired(required = false)
	private AllianceDao allianceDao;

	@Autowired(required = false)
	private TenderDao tenderDao;

	@Autowired(required = false)
	private OrderDao orerDao;

	@Override
	public List<Alliance> getList(Long uid, Integer limit, Integer end, String aname)
	{
		return allianceDao.getList(uid, limit, limit*(end-1), aname);
	}

	@Override
	public List<Alliance> getList2(Long uid, Integer limit, Integer end, String aname)
	{
		return allianceDao.getList2(uid, limit, limit*(end-1), aname);
	}

	@Override
	public String getServerValue(String sname)
	{
		return allianceDao.getServerValue(sname);
	}

	@Override
	public Alliance getAlliance(Long aid)
	{
		return allianceDao.getAlliance(aid);
	}

	@Override
	public boolean checkAlliance(Long uid,Long aid)
	{
		boolean flag=false;
		int n=allianceDao.checkAlliance(uid,aid);
		if(n>0)
		{
			flag=true;
		}
		return flag;
	}

	@Override
	public boolean deleteAlliance(Long aid)
	{
		boolean flag=false;
		int n=allianceDao.deleteAlliance(aid);
		if(n>0)
		{
			flag=true;
		}
		return flag;
	}

	@Override
	public List<User_alliance> getUsers(Long aid)
	{
		return allianceDao.getUsers(aid);
	}

	@Override
	public boolean updateState(Long aid, Long uid, String state)
	{
		boolean flag=false;
		int n=allianceDao.updateState(aid, uid, state);
		if(n>0)
		{
			flag=true;
		}
		return flag;
	}

	@Override
	public boolean checkAccount(String account)
	{
		boolean flag=false;
		int n=allianceDao.checkAccount(account);
		if(n==0)
		{
			flag=true;
		}
		return flag;
	}

	@Override
	public boolean checkOrder(Long aid)
	{
		boolean flag=true;
		List<Long> list=tenderDao.getNeeds(aid);
		out:for (Long nid : list)
		{
			List<Long> list2=orerDao.getState(nid);
			in :for (Long pid : list2)
			{
				if(pid==6 || pid==8 || pid==9 || pid==11)
				{
					flag=false;
					break out;
				}
			}
		}
		return flag;
	}

	@Override
	public boolean backAlliance(Long aid, Long uid)
	{
		boolean flag=false;
		int n= allianceDao.backAlliance(aid, uid);
		if(n>0)
		{
			flag=true;
		}
		return flag;
	}

	@Override
	public User_alliance getUa(Long aid, Long uid)
	{
		return allianceDao.getUa(aid, uid);
	}

	@Override
	public boolean deleteAll(Long aid)
	{
		boolean flag=false;
		int n=allianceDao.deleteAll(aid);
		if(n>0)
		{
			flag=true;
		}
		return flag;
	}

	@Override
	public Long addAlliance(Alliance alliance)
	{
		return allianceDao.addAlliance(alliance);
	}

	@Override
	public int getSum(Long uid, String aname)
	{
		return allianceDao.getSum(uid, aname);
	}

	@Override
	public int getSum2(Long uid, String aname)
	{
		return allianceDao.getSum2(uid, aname);
	}

	@Override
	public List<Alliance> getMyAlliance(Long uid)
	{
		return allianceDao.getMyAlliance(uid);
	}

	@Override
	public boolean addUserAlliance(User_alliance ua)
	{
		boolean flag=false;
		int n=allianceDao.addUserAlliance(ua);
		if(n>0)
		{
			flag=true;
		}
		return flag;
	}

	@Override
	public String getServerType(Long sid)
	{
		return allianceDao.getServerType(sid);
	}
}